package global;

import va_file.VAFile;

public class Commands 
{
	public static boolean verbose = true;

	/* MY
	 * Phase 2 version
	 * Method will parse and execute a batchinsert command.
	 * Optional 4th argument can be used to set the number of bits per dimension
	 */
	public static void batchCommand(String[] args)
	{
		if (args.length >= 3)
		{
			try
			{
				if (args.length > 3)	// number of bits to use was passed
				{
					int bits;
					try
					{
						bits = Integer.parseInt(args[3]);
						if (bits < 1 || bits > 16)
						{
							bits = -1;
						}
					}
					catch (Exception e)
					{
						bits = -1;
					}
					if (bits < 0)
					{
						VAFile.BITS_PER_DIMENSTION = VAFile.DEFAULT_BITS;
						System.out.format("Specified bits is invalid,  will use default value: %d\n", VAFile.BITS_PER_DIMENSTION);
					}
					else
					{
						VAFile.BITS_PER_DIMENSTION = bits;
						System.out.format("Using %d bits per dimension.\n", VAFile.BITS_PER_DIMENSTION);
					}
				}
				CommandLine.initializeDB(false);
				Batch.OldInsert(args[1], args[2]);
			}
			catch (Exception e)
			{
				System.err.println("Error inserting records: " + e.getMessage());
			}
		}
		else
		{
			System.out.println("Invalid format for oldinsert command.");
			System.out.println("Expected format is: oldinsert DATAFILENAME DNAME");
		}
	}

    // Creates a new relation (and potentially a new database as well)

	public static void batchCreate(String[] args)
	{
		boolean useExistingDB = true;
		if (args.length >= 3)
		{
			if (args.length > 3 && args[3].equalsIgnoreCase("Y"))
			{
				// optional parameter passed to create new database
				useExistingDB = false;
			}
			try
			{
				CommandLine.initializeDB(useExistingDB);
				Batch.Create(args[1], args[2]);
			}
			catch (Exception e)
			{
				System.err.println("Error creating database: " + e.getMessage());
			}
		}
		else
		{
			System.out.println("Invalid format for batchcreate command.");
			System.out.println("Expected format is: batchcreate DATAFILENAME RELNAME {CreateNewDB(Y|N)}");
		}
	}

    // Deletes tuples from a relation and associated indexes

    public static void batchDelete(String[] args)
    {
        if (args.length == 3)
        {
            try
            {
                CommandLine.initializeDB(true);
                BatchDelete.Delete(args[1], args[2]);
            }
            catch (Exception e)
            {
                System.err.println("Error deleting tuples: " + e.getMessage());
            }
        }
        else
        {
            System.out.println("Invalid format for batchdelete command.");
            System.out.println("Expected format is: batchdelete UPDATEFILENAME RELNAME");
        }
    }

    // Inserts tuples into a relation and update associated indexes

	public static void batchInsert(String[] args)
	{
		if (args.length >= 3)
		{
			boolean recreateIndexes = false;
			if (args.length > 3 && args[3].equalsIgnoreCase("Y"))
			{
				// optional parameter was passed to recreate indexes
				recreateIndexes = true;
			}
			try
			{
				CommandLine.initializeDB();
				Batch.Insert(args[1], args[2], recreateIndexes);
			}
			catch (Exception e)
			{
				System.err.println("Error creating database: " + e.getMessage());
			}
		}
		else
		{
			System.out.println("Invalid format for batchinsert command.");
			System.out.println("Expected format is: batchinsert DATAFILENAME RELNAME {RecreateIndexes (Y|N)}");
		}
	}

    // Creates a VA index on a relation

	public static void vaIndex(String[] args)
	{
		boolean validCommand = false;
		int colID=0, bits=0;
		if (args.length >= 5 && (args[3].equalsIgnoreCase("H") || args[3].equalsIgnoreCase("B")))
		{
			validCommand = true;
			try
			{
				colID = Integer.parseInt(args[2]);
				bits = Integer.parseInt(args[4]);
			}
			catch (Exception e)
			{
				validCommand = false;
			}
		}
		if (validCommand)
		{
			try
			{
				CommandLine.initializeDB();
				IDXSchema index = new IDXSchema(args[1], colID, args[3], bits);
				BatchIndex.CreateIndex(args[1], index);
			}
			catch (Exception e)
			{
				System.err.println("Error creating index: " + e.getMessage());
			}
		}
		else
		{
			System.out.println("Invalid format for vaindex command.");
			System.out.println("Expected format is: vaindex RELNAME COLUMNID(int) INDEXTYPE(H|B) B(int)");
		}
	}
}
